package com.newenergy.wisehome.ui.fragment;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.newenergy.wisehome.R;
import com.newenergy.wisehome.config.AppManager;
import com.newenergy.wisehome.data.bean.BallLight;
import com.newenergy.wisehome.data.bean.DeviceCategory;
import com.newenergy.wisehome.data.bean.Family;
import com.newenergy.wisehome.data.ormlite.dao.DeviceCategoryDao;
import com.newenergy.wisehome.ui.adapter.NewHandsetRoomAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import me.yokeyword.fragmentation.ISupportFragment;

/**
 *
 * Created by kyx on 2018/1/30.
 */

public class HandsetShareFragment extends BaseFragment {

    private static String ARG_HANDSETSHARE="arg_handsetshare";
    @BindView(R.id.rl_one)
    RelativeLayout rlOne;
    @BindView(R.id.tv_back)
    TextView mTvBack;
    @BindView(R.id.tv_page)
    TextView mTvPage;
    @BindView(R.id.btn_add)
    Button mBtnAdd;
    @BindView(R.id.btn_lighting_share)
    Button mBtnShare;
    @BindView(R.id.tv_one)
    TextView mTvOne;


    private Family mCurrFamily;
    private int tvposition = 0;
    private Dialog mDialog;
    private List<BallLight> mFamilyGroups;
    private List<DeviceCategory> mFamilyDeviceCategorys;
    private LinearLayoutManager mLayoutManager;


    //分别记录当前的房间id
    private String groupId1="00";
    private String deviceType="7201";


    @Override
    public int getLayoutId() {
        return R.layout.fragment_handle_share;
    }

    public static ISupportFragment newInstance(Family currItem) {
        Bundle args = new Bundle();
        args.putParcelable(ARG_HANDSETSHARE, currItem);
        HandsetShareFragment fragment = new HandsetShareFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void init(View rootView) {
       /* mBtnAdd.setVisibility(View.VISIBLE);
        mBtnAdd.setBackgroundResource(R.drawable.btn_share);*/
    }

    @Override
    public void onResume() {
        super.onResume();
        mCurrFamily = getArguments().getParcelable(ARG_HANDSETSHARE);
        mTvPage.setText(getString(R.string.btn_share_handset));
        String familyId = mCurrFamily.getFamilyId();
       /* BallLightDao ballLightDao = new BallLightDao(getContext());
        mFamilyGroups = ballLightDao.getFamilyBallLights(familyId,1);*/

        DeviceCategoryDao deviceCategoryDao = new DeviceCategoryDao(getContext());
        mFamilyDeviceCategorys = deviceCategoryDao.getFamilyDeviceCategorys(familyId);

        if (groupId1.equals("00")) {
            mBtnShare.setBackgroundResource(R.drawable.btn_custom_p);
        }else {
            mBtnShare.setBackgroundResource(R.drawable.btn_custom_selector);
        }

    }

    @OnClick({R.id.btn_back, R.id.tv_back, R.id.btn_lighting_share, R.id.rl_one})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                doBack();
                break;
            case R.id.tv_back:
                doBack();
                break;
            case R.id.btn_lighting_share:
                if (groupId1.equals("00")) {
                    showDialog(getString(R.string.please_add_group));

                }else {
                    shareFamilyGroup();
                    showDialog(getString(R.string.sharing_success));

                }
                mHandler.sendEmptyMessageDelayed(1, 1000);
                break;

            case R.id.rl_one:
                tvposition = 0;
                showBottomDialog();
                break;

        }
    }


    //发送数据包含新的加密种子跟参数，以默认的加密种子和参数进行加密
    private void shareFamilyGroup() {
        int seed=mCurrFamily.getSeed();
        int feedback=mCurrFamily.getFeedback();
        String cmd=intToHex(seed)+numToHexs(feedback);
        String data1=mCurrFamily.getFamilyId()+groupId1+"000000"+"00"+cmd;
        AppManager.getInstance(mainActivity).remoteShareCmd("AA",data1,73,4777,deviceType);
    }


    public void showBottomDialog() {
        View contenView = View.inflate(getContext(), R.layout.dialog_share_room, null);

        mDialog = new Dialog(getContext(), R.style.style_dialog);

        Window window = mDialog.getWindow();
        window.setGravity(Gravity.CENTER);
        window.getDecorView().setPadding(0, 0,0, 0);

        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = dpToPx(300);
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);

        EasyRecyclerView mRecyclerView = (EasyRecyclerView) contenView.findViewById(R.id.recyclerView);
        TextView tvNoneName = (TextView) contenView.findViewById(R.id.tv_none_name);
      /*  HandsetRoomAdapter handsetRoom=new HandsetRoomAdapter(getContext().getApplicationContext());
        handsetRoom.refreshData(mFamilyGroups);*/
        NewHandsetRoomAdapter handsetRoom = new NewHandsetRoomAdapter(getContext().getApplicationContext());
        handsetRoom.refreshData(mFamilyDeviceCategorys);


        mLayoutManager = new LinearLayoutManager(getContext());
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(handsetRoom);

        mDialog.setContentView(contenView);
        mDialog.show();

        tvNoneName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (tvposition){
                    case 0:
                        groupId1="00";
                        mTvOne.setText(getString(R.string.none));
                        mDialog.dismiss();
                        mBtnShare.setBackgroundResource(R.drawable.btn_custom_p);
                        break;

                }
            }
        });



        handsetRoom.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                switch (tvposition){
                    case 0:
                      /*  groupId1=mFamilyGroups.get(position).getGroupId();
                        mTvOne.setText(mFamilyGroups.get(position).getName());*/

                        groupId1=mFamilyDeviceCategorys.get(position).getGroupId();

                        int category = mFamilyDeviceCategorys.get(position).getCategory();
                        if (category == 1) {
                            deviceType = "7201";
                        } else if (category == 2) {
                            deviceType = "7202";
                        }else if (category == 3) {
                            deviceType = "7203";
                        }else if (category == 4) {
                            deviceType = "7204";
                        }else if (category == 5) {
                            deviceType = "7205";
                        }else if (category == 6) {
                            deviceType = "7206";
                        }else if (category == 7) {
                            deviceType = "7207";
                        }else if (category == 8) {
                            deviceType = "7208";
                        }else if (category == 9) {
                            deviceType = "7209";
                        }else if (category == 10) {
                            deviceType = "720A";
                        }else if (category == 11) {
                            deviceType = "720B";
                        }else if (category == 12) {
                            deviceType = "720C";
                        }else if (category == 13) {
                            deviceType = "720D";
                        }

                        mTvOne.setText(mFamilyDeviceCategorys.get(position).getName());


                        mBtnShare.setBackgroundResource(R.drawable.btn_custom_selector);
                        mDialog.dismiss();
                        break;

                }
            }
        });

    }


    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    dissDialog();
                    break;

            }
        }
    };


}
